var MenuGlobal={
};

$(document).ready(function(){
	$('#menuTree').tree({    
	    url: mainDomain+'/menu/selectAllMenu.action',  
	    lines:true,
	    animate:true,
	    method:'get',
	    checkbox:true,
	    dnd:true,
	    onBeforeDrag:function (node){
	    	var nodeData=node.attributes;
	    	if(nodeData.menuParentId==0){
	    		return false;
	    	}
	    },
	    onBeforeDrop:function (target, source, point){
	    	var sourceData=source.attributes;
	    	var targetData=$('#menuTree').tree('getNode',target).attributes;
	    	if(sourceData.menuParentId==0){
	    		return false;
	    	}
	    	if(sourceData.menuType==0 && targetData.menuParentId==0){
	    		return false;
	    	}
	    	if(sourceData.menuType==0 && targetData.menuType==0){
	    		return false;
	    	}
	    	if(sourceData.menuType==1 && targetData.menuParentId!=0){
	    		return false;
	    	}
	    	if(targetData.menuType==0){
	    		return false;
	    	}
	    },
	    onDrop:function (target, source, point){
	    	var sourceData=source.attributes;
	    	var targetData=$('#menuTree').tree('getNode',target).attributes;
	    	var menu={id:sourceData.id,menuParentId:targetData.id};
	    	$.post(mainDomain+'/menu/updateMenu.action',menu,function(rs){
	    		if(rs.success){
	    			$('#menuTree').tree('reload');
	    		}
	    	},'json');
	    },
	    formatter:function(node){
	    	var menu=node.attributes;
	    	var opts=[];
	    	if(menu.isVisible==0){
	    		opts.push('<span style="font-size:16px;color:gray;">'+node.text+'</span>');
	    	}else{
	    		opts.push('<span style="font-size:16px;">'+node.text+'</span>');
	    	}
	    	opts.push('<span class="tree-opt"><div class="divhr————————"></div>');
	    	if(menu.menuType==1){
	    		//opts.push('<span title="新增子菜单" style="margin-left:10px;" class="iconspan icon-add" onclick="openAddSubMenu('+node.id+')"></span>');
	    		//opts.push('<span title="编辑" style="margin-left:5px;" class="iconspan icon-edit" onclick="openUpdateMenu('+node.id+')"></span>');
	    	}else{
	    		//opts.push('<span title="编辑" style="margin-left:5px;" class="iconspan icon-edit" onclick="openUpdateMenu('+node.id+')"></span>');
	    	}
	    	
	    	// 更具页面隐藏的menuIds字符串，判断哪些节点需要选中
	    	$.each($('#menuIds').val().split(","), function(key, val) {
	    	    //console.log('index in arr:' + key + ", corresponding value:" + val);
	    	    if(menu.isLeaf && menu.id == val) {
	    	    	node.checked=true;
	    	    }
	    	});
	    	
	    	opts.push('</span>');
			return opts.join('');;
		},
	    loadFilter: function(data,parent){   
	    	var treeJsons=[];
	    	var L1=[];
	    	var other=[];
	    	var menuMap={};
	    	if(!$.isEmptyObject(data)){
	    		$.each(data,function(i,v){
	    			var nodeJson={};
	    			nodeJson.id=v.id;
	    			nodeJson.text=v.menuName;
	    			nodeJson.state='open';
	    			nodeJson.attributes=v;
	    			nodeJson.children=[];
	    			if(v.menuParentId==0){
	    				L1.push(nodeJson);
	    			}else{
	    				other.push(nodeJson);
	    				if(v.menuParentId in menuMap){
	    					menuMap[v.menuParentId].push(nodeJson);
	    				}else{
	    					menuMap[v.menuParentId]=[];
	    					menuMap[v.menuParentId].push(nodeJson);
	    				}
	    			}
	    		});
	    		for(var j=0;j<other.length;j++){
	    			if(other[j].id in menuMap){
	    				other[j].children=menuMap[other[j].id];
	    			}
	    		}
	    		for(var i=0;i<L1.length;i++){
	    			var nodeData=L1[i];
	    			if(nodeData.id in menuMap){
	    				nodeData.children=menuMap[nodeData.id];
	    			}
	    			treeJsons[i]=nodeData;
	    		}
	    	}
	    	return treeJsons;
	    }    
	});
	
	//提交选项事件绑定
	$('#menuAddBtn').click(function(){
		//var nodes = $('#menuTree').tree('getChecked');
		var nodes = $('#menuTree').tree('getChecked', ["checked",'indeterminate']);  // 选中子节点，半选状态的父节点也取到

		
		// 选中的menu
		var menuIdLst=[];
		for(var i=0;i<nodes.length;i++) {
			menuIdLst.push(nodes[i].id);
		}
		
		//alert(menuIdLst); return;
		
		// 参数
		var roleMenuDTO = {};
		roleMenuDTO.roleId=$('#roleId').val();  //alert($('#roleId').val()); return;
		roleMenuDTO.menuIdLst=menuIdLst;
		
		// 调用
		$.postJSON(mainDomain+'/role/saveRole2Menu.action',roleMenuDTO,function(rs){
			if(rs.success) {
				alert('success');
			}
		});
	});
});


